<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Vue中动画封装</title>
    <script src="./js/vue.js"></script>
    <style>
        .v-enter, .v-leave-to{
            opacity: 0;
        }
        .v-enter-active, .v-leave-active{
            transition: 1s opacity;
        }
    </style>
</head>
<body>
    <div id="root">
        <!-- 把动画封装在组件中，使用动画是只需要调用该组件即可 这是第一种封装方法 -->
        <fade  :show="show">
            <div>hello vue</div>
        </fade>
        <fade :show="show">
            <h2>hello vue h2</h2>
        </fade>
        <color-change :show="show">
            <h2>hello vue h2</h2>
        </color-change>
        <button @click="handleBtnClick">toggle</button>
    </div>
    <script>
        Vue.component('fade', {
            props: ['show'],
            template: `
                <transition>
                    <slot v-if="show"></slot>
                </transition>
            `
        });
        Vue.component('color-change', {
            props: ['show'],
            template: `
                <transition 
                    @before-enter="handleBeforeEnter"
                    @enter="handleEnter"
                    @after-enter="handleAfterEnter"
                >
                    <slot v-if="show"></slot>
                </transition>
            `,
            methods: {
                handleBeforeEnter: function(el){
                    el.style.color = "red";
                },
                handleEnter: function(el, done){
                    setTimeout(() => {
                        el.style.color="green";
                    }, 2000);

                    setTimeout(() => {
                        done();
                    }, 4000);
                },
                handleAfterEnter: function(el){
                    el.style.color = "black";
                }
            }
        })
        var vm = new Vue({
            el: "#root",
            data: {
                show: true,
            },
            methods: {
                handleBtnClick: function(){
                    this.show = !this.show;
                }
            }
        });
    </script>

    
</body>
</html>